' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  Solution Accelerator for Business Desktop Deployment
' // File:      Credentials_scripts.vbs
' //
' // Purpose:   New Credentials
' //
' // Microsoft Solution Version:  3.0.134
' // Microsoft Script Version:    3.0.134
' // Customer Build Version:      1.0.0
' // Customer Script Version:     1.0.0
' //
' // Microsoft History:
' // 2.6.0    KeithGa  03/24/2006  Ported version, new script header
' // 3.0.114  MTN      11/21/2006  Cleanup
' // 3.0.134  MTN      01/02/2007  Moved CredentialCheck logic to WizUtility
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************

Option explicit

Dim sValidateUNC
Dim sValidateDomain
Dim bDoNotSaveParameters
Dim bLeaveShareOpen


Function UserCredentialsInitialize
	Dim oArguments
	Dim SWbemObject
	Dim sNewDomain
	Dim aUNCPath


	' Parse the Command Line

	Set oArguments = ParseCommandLine(oWizard.CommandLine)   
    
	sValidateDomain = GetNamedParameter(oArguments, "/ValidateAgainstDomain")
	aUNCPath = Split(GetNamedParameter(oArguments, "/ValidateAgainstUNCPath"),"\")
	bDoNotSaveParameters = not IsEmpty(GetNamedParameter(oArguments, "/DoNotSave"))
	bLeaveShareOpen = not IsEmpty(GetNamedParameter(oArguments, "/LeaveShareOpen"))

	oEnvironment.Item("UserCredentials") = CStr(FALSE)
	
	If UBound(aUNCPath) > 2 then
		If aUNCPath(0)="" and aUNCPath(1)="" and aUNCPath(2)<>"" and aUNCPath(3)<>"" then
			sValidateUNC = "\\" & aUNCPath(2) & "\" & aUNCPath(3)
			oLogging.CreateEntry "Validate Against UNC: " & sValidateUNC , LogTypeInfo
		End if
	End if

	If not IsEmpty(sValidateDomain) then

		oLogging.CreateEntry "Validate Against Domain: " & sValidateDomain , LogTypeInfo

	ElseIf not IsEmpty(sValidateUNC) then 

		sValidateDomain = GetDomainDefault
		oLogging.CreateEntry "Validate Against UNC: " & sValidateUNC , LogTypeInfo
		' Do we need to do any checking for IP address ( as compared to server names )       

	Else

		sValidateDomain = GetDomainDefault

	End if
    
	If UserName.Value = "" and oNetwork.UserName <> "" and oNetwork.UserName <> "SYSTEM" then
		UserName.Value = oNetwork.UserName
	End if
    
    	If oProperties("userdomain") = "" and sValidateDomain <> "" then
		userdomain.Value = sValidateDomain
	ElseIf oproperties("userdomain") = "" and oNetwork.UserDomain <> "" then
		userdomain.Value = oNetwork.UserDomain
	End if
    
End function


Function ValidateCredentials
	Dim r

	InvalidCredentials.style.display = "none"
	ValidateCredentials = ParseAllWarningLabelsEx(userdomain, username)

	If ValidateCredentials then

		r = CheckCredentials(sValidateUNC, UserName.value, userdomain.Value, userpassword.value) 
		If r <> TRUE then
			InvalidCredentials.innerText = "* Invalid credentials: " & r
			InvalidCredentials.style.display = "inline"
			ValidateCredentials = false
		End if

	End if

End Function
